home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj1291.zip / BOOK.ASC < prev    next >
Text File  |  1991-11-11  |  1KB  |  58 lines

  1. BOOKSHELF.ASC
  2. Title: PROGRAMMER'S BOOKSHELF
  3. Source code that accompanies Andrew Schulman's book review of 
  4. "The Standard C Library" (Plauger) and "The C++ Programming 
  5. Language, Second Edition" (Stroustrup).
  6.  
  7.  
  8. Example 1: A stack template in C++ 3.0
  9.  
  10. // stack.h
  11.  
  12. template<class T> class stack
  13. {
  14.     T *v, *p; 
  15.     int sz;
  16. public:
  17.     // all the following functions are inline
  18.     stack(int s)        { v = p = new T[sz = s]; }
  19.     ~stack()            { delete[] v; }
  20.     void push(T a)      { *p++ = a; }
  21.     T pop()             { return *--p; }
  22.     int size() const    { return p-v; }
  23. }
  24.  
  25.  
  26.  
  27. Example 2: Creating two different classes of stack, from the same 
  28. template
  29.  
  30. #include <iostream.h>
  31. #include "stack.h"
  32.  
  33. main()
  34. {
  35.     stack<int>  si(100);        // stack of 100 ints
  36.     stack<char> sc(100);        // stack of 100 chars
  37.  
  38.     for (int i=0; i<10; i++)
  39.     {
  40.         si.push(i);
  41.         sc.push(i);
  42.     }
  43.  
  44.     while (si.size())           // pop everything and
  45.         cout << si.pop() << ' ' ;   // display it
  46.     cout << '\n' ;
  47. }
  48.  
  49.  
  50. Example 3 
  51.  
  52.  
  53. mov bx, word ptr [si.p]     
  54. mov ax, word ptr [i]
  55. mov word ptr [bx], ax       ; *p = i
  56. add word ptr [si.p], 2      ; p += sizeof(int)
  57.  
  58.